# @Author ZhangGJ
# @Date 2021/12/03 18:36

import cv2.cv2 as cv2


def video():
    capture = cv2.VideoCapture(0)
    print(capture.isOpened())
    capture.release()


def video2():
    capture = cv2.VideoCapture(0)
    while (capture.isOpened()):
        retval, image = capture.read()
        cv2.imshow('Video', image)
        key = cv2.waitKey(1)
        if key == 32:
            break
    fps = capture.get(cv2.CAP_PROP_FPS)
    frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
    frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)

    print('帧速率', fps)
    print('帧数', frame_count)
    print('帧宽度', frame_width)
    print('帧高度', frame_height)
    capture.release()
    cv2.destroyAllWindows()


# 彩色转为灰度视频
def video3():
    capture = cv2.VideoCapture(0)
    while capture.isOpened():
        retval, image = capture.read()
        image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        if retval is True:
            cv2.imshow('Video', image)
            cv2.imshow('Video_Gray', image_gray)
        key = cv2.waitKey(1)
        if key == 32:
            break
    capture.release()
    cv2.destroyAllWindows()


# 保存某一段时间图像
def video4():
    cap = cv2.VideoCapture(0)
    while cap.isOpened():
        ret, frame = cap.read()
        cv2.imshow('Video', frame)
        k = cv2.waitKey(1)
        if k == 32:
            cap.release()
            cv2.destroyWindow('Video')
            cv2.imwrite('../images/save/copy.jpg', frame)
            cv2.imshow('img', frame)
            cv2.waitKey()
            break
    cv2.destroyAllWindows()


# 读取视频
def read_video():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    while video.isOpened():
        retval, image = video.read()
        cv2.namedWindow('Video', 0)
        cv2.resizeWindow('Video', 420, 300)
        if retval == True:
            cv2.imshow('Video', image)
        else:
            break
        key = cv2.waitKey(50)
        if key == 27:
            break
    video.release()
    cv2.destroyAllWindows()


def read_video2():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    while video.isOpened():
        retval, img_color = video.read()
        cv2.namedWindow('Gray', 0)
        cv2.resizeWindow('Gray', 420, 300)
        if retval is True:
            img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
            cv2.imshow('Gray', img_gray)
        else:
            break
        key = cv2.waitKey(1)
        if key == 27:
            break
    video.release()
    cv2.destroyAllWindows()


def read_video3():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    while video.isOpened():
        retval, image = video.read()
        cv2.namedWindow('Video', 0)
        cv2.resizeWindow('Video', 420, 300)
        if retval is True:
            cv2.imshow('Video', image)
        else:
            break
        key = cv2.waitKey(50)
        if key == 32:
            cv2.waitKey(0)
            continue
        if key == 27:
            break
    video.release()
    cv2.destroyAllWindows()


# 获取视频文件属性
def read_video4():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    fps = video.get(cv2.CAP_PROP_FPS)
    frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
    frame_width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
    frame_height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)

    print('帧速率', fps)
    print('帧数', frame_count)
    print('帧宽度', frame_width)
    print('帧高度', frame_height)


# 动态显示属性
def read_video5():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    fps = video.get(cv2.CAP_PROP_FPS)
    frame_num = 1
    while video.isOpened():
        retval, frame = video.read()
        cv2.namedWindow('Video', 0)
        cv2.resizeWindow('Video', 420, 300)
        if retval is True:
            cv2.putText(frame, 'frame: ' + str(frame_num), (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,
                        (0, 0, 255), 5)
            cv2.putText(frame, 'second: ' + str(round(frame_num / fps, 2)) + 's', (0, 200),
                        cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
            cv2.imshow('Video', frame)
        else:
            break
        key = cv2.waitKey(50)
        frame_num += 1
        if key == 27:
            break
    video.release()
    cv2.destroyAllWindows()


# 保存视频
def save_video():
    capture = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
    output = cv2.VideoWriter('../video/output.avi', fourcc, 20, (1280, 720))
    while capture.isOpened():
        retval, frame = capture.read()
        if retval is True:
            output.write(frame)
            cv2.imshow('frame', frame)
        key = cv2.waitKey(1)
        if key == 27:
            break
    capture.release()
    output.release()
    cv2.destroyAllWindows()


def save_video2():
    capture = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
    fps = 20
    output = cv2.VideoWriter('../video/ten_seconds.avi', fourcc, fps, (1280, 720))
    frame_num = 10 * fps
    while capture.isOpened() and frame_num > 0:
        retval, frame = capture.read()
        if retval is True:
            output.write(frame)
            cv2.imshow('frame', frame)
        key = cv2.waitKey(1)
        frame_num -= 1
    capture.release()
    output.release()
    cv2.destroyAllWindows()


def save_video3():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    fps = video.get(cv2.CAP_PROP_FPS)
    size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
    output = cv2.VideoWriter('../video/copy.avi', fourcc, fps, size)
    while video.isOpened():
        retval, frame = video.read()
        if retval is True:
            output.write(frame)
        else:
            break
    print('已经保存')
    video.release()
    output.release()


def save_video4():
    video = cv2.VideoCapture('../video/公司宣传.avi')
    fps = video.get(cv2.CAP_PROP_FPS)
    size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
    output = cv2.VideoWriter('../video/ten_seconds2.avi', fourcc, fps, size)
    frame_num = 10 * fps
    while video.isOpened() and frame_num > 0:
        retval, frame = video.read()
        if retval is True:
            output.write(frame)
        frame_num -= 1
    print('Video already save')
    video.release()
    output.release()


if __name__ == '__main__':
    save_video2()
